<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		
		$this->load->config('imageene', TRUE);
		$this->load->helper('cookie');
		
		// set the default language to english
		$lang = 'en_us';
		
		// check for any cookies available
		if(get_cookie('uilang', TRUE)) {
			$lang = get_cookie('uilang', TRUE);
		}
		
		// if no cookie, check if there is a language session
		else if($this->session->userdata('uilang') != "") {
			$session_uilang = $this->session->userdata('uilang');
			
			if($this->imageene->is_language_available($session_uilang) !== FALSE) {
				$lang = $session_uilang;
			}
		}
		
		// if no language defined in session or cookies detect the
		else {
			$browser_lang = getDefaultLanguage();
			
			if($browser_lang == "en") $browser_lang = "en_us";
			if($browser_lang == "es") $browser_lang = "es_es";
			if($browser_lang == "fr") $browser_lang = "fr_fr";
			
			if($this->imageene->is_language_available($browser_lang) !== FALSE) {
				$lang = $browser_lang;
			}
		}
		
		// set the language session variable and cookie
		$this->session->set_userdata(array('uilang' => $lang));

		set_cookie(array(
			'name' 		=> "imageene_uilang",
			'value'		=> $lang,
			'expire'	=> 60*60*24*31*12
		));
		
		// set the locale
		setlocale(LC_TIME, $lang);
		
		// load the matching language
		$this->lang->load('common', $this->imageene->is_language_available($this->session->userdata('uilang')));
	}
	
	public function index() {
	}
}
?>